home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swaga_c.zip / CMDLINE.SWG / 0012_Command Line Unit 2.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  15KB  |  371 lines

  1. {
  2. This unit will allow you to access the original command line as it was
  3. originally entered by the user.  Here is the source code for the CmdLine
  4. object.  It was developed by Computer Mavericks, using information gleened
  5. from the info-pascal internet forum.  In the spirit of the forum, this is
  6. offered into the public domain.  If you use it, think kind thoughs of Lee
  7. Crites and the small staff here at CM.
  8.  
  9. This was written using Borland Pascal 7.0's BPW in Real Mode.  (after all,
  10. you'll probably not have to many command line parameters to check if you
  11. are working in Windows or OS2, right???  It requires the STRINGS unit that
  12. comes with BP7.  If you are working in TP6/TP5.5, and don't have access to
  13. this, we do (should I say <did>) have a unit for doing null terminated
  14. strings for each of those releases that we might be able to send out.  As
  15. I remember, we took the BPW 1.0 Strings unit and played around with it
  16. until it compiled in TP6, so I don't know which version that I still have
  17. access to (it's been a while since I looked at our last tp6 archives).
  18.  
  19. I threw this together over the weekend, and tested it using the whoami
  20. program and test.bat file.  There might still be a problem floating around
  21. in there somewhere.  This is about the first time that I've really sent
  22. out some source code like this, and really haven't gone through it with a
  23. fine tooth comb.  If there are bugs or (more importantly) imporvements
  24. that some of you can see, please let me know.
  25. }
  26.  
  27. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  28. { =-=-=-=-=                                                   =-=-=-=-= }
  29. { =-=                                                               =-= }
  30. {                               CMDLINE.PAS                             }
  31. { This unit contains the following:                                     }
  32. {    -- CMDLINE, a mute object that will parse the physical command     }
  33. {       line as input by the user, and return the information that was  }
  34. {       requested.                                                      }
  35. { =-=                                                               =-= }
  36. { =-=-=-=-=                                                   =-=-=-=-= }
  37. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  38. {$D-} { debugging information off }
  39. {$X+} { allow extended syntax }
  40. {$V+} { require same var type }
  41. {$R-} { range checking off }
  42. {$E+} { Add 8087 software emulation code }
  43. {$N+} { Use the 8087 software emulation code }
  44. {$I-} { Enable IOResult for I/O checking }
  45. {$B-} { Short-cut boolean evauation }
  46. {$O+} { Allow this to be a part of an overlay }
  47. Unit CmdLine;
  48. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  49. Interface
  50. Uses Strings;  { Strings is a BP7 unit.  I have a TP6 version available }
  51. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  52. Type
  53.   TCmdLine = Object
  54.      Constructor Init;
  55.      Destructor Done; Virtual;
  56.      { this will return the information requested -- the whole reason   }
  57.      { for doing this in the first place.  It will return TRUE if the   }
  58.      { ParmStrIn was found, otherwise false. This way you can check for }
  59.      { switches entered with no data, since StrBack would otherwise be  }
  60.      { null.                                                            }
  61.      Function  GetParameter(ParmStrIn:String;Var StrBack:String):Boolean;
  62.      Function  GetCommandLine:String;                 { the entire line }
  63.      Function  GetActualProgram:String;       { the actual name entered }
  64.      Function  GetCallingProgram:String;     { the fully qulaified name }
  65.      Function  GetLaunchingProgram:String; { what environment called me }
  66.      Procedure Trim;        { remove leading, trailing, multiple spaces }
  67.      Procedure Capitalize;                   { just what it sounds like }
  68.      Procedure Restore;                 { restores the original version }
  69.      Function  GetDivideChars:String;      { returns the dividing chars }
  70.      Procedure SetDivideChars(NewDivideChars:String);       { sets them }
  71.  
  72.      Private
  73.      DivideChars:String;             { the chars that signal a new parm }
  74.      CommandLine,OriginalCommandLine:String;  { just what the name says }
  75.      LaunchingProgram,CallingProgram,ActualProgram:String;
  76.      End; { TCmdLine }
  77. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  78. Var
  79.   CommandLine:TCmdLine;
  80. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  81. {PAGE}
  82. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  83.  
  84.  
  85. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  86. Implementation
  87. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  88.  
  89.  
  90. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  91. Const
  92.   NONE = '<NONE>';
  93. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  94.  
  95.  
  96. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  97. Procedure MakeCaps(Var ss:String);
  98. Var xx,ll:Byte;
  99. Begin
  100.   ll := Length(ss);
  101.   For xx := 1 to ll Do ss[xx] := UpCase(ss[xx]);
  102. End; { MakeCaps }
  103. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  104.  
  105.  
  106. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  107. Procedure TrimString(Var ss:String);
  108. Var xx:Integer;
  109. Begin
  110.   If (length(ss) < 1) Then exit;
  111.   { remove leading spaces }
  112.   While (ss[1] = chr(32)) Do Begin delete(ss,1,1);
  113.   If (length(ss) < 1) Then exit; End;
  114.   { remove trailing spaces }
  115.   While (ss[length(ss)] = chr(32)) Do Delete(ss,length(ss),1);
  116.   { remove imbedded spaces }
  117.   xx := pos('  ',ss);
  118.   While (xx <> 0) Do Begin Delete(ss,xx,1); xx := Pos('  ',ss); End;
  119. End; { TrimString }
  120. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  121.  
  122.  
  123. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  124. Procedure TCmdLine.Capitalize;
  125. Begin
  126.   MakeCaps(CommandLine);
  127. End; { Capitalize }
  128. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  129.  
  130.  
  131. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  132. Destructor TCmdLine.Done;   { I'm not sure what can/should be done here }
  133. Begin
  134. End; { CmdLine }
  135. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  136.  
  137.  
  138. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  139. Function TCmdLine.GetActualProgram:String;
  140. Begin
  141.   GetActualProgram := ActualProgram;
  142. End; { GetActualProgram }
  143. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  144.  
  145.  
  146. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  147. Function TCmdLine.GetCallingProgram:String;
  148. Begin
  149.   GetCallingProgram := CallingProgram;
  150. End; { GetCallingProgram }
  151. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  152.  
  153.  
  154. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  155. Function TCmdLine.GetCommandLine:String;
  156. Begin
  157.   GetCommandLine := CommandLine;
  158. End; { GetCommandLine }
  159. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  160.  
  161.  
  162. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  163. Function TCmdLine.GetDivideChars:String;
  164. Begin
  165.   GetDivideChars := DivideChars;
  166. End; { GetDivideChars }
  167. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  168.  
  169.  
  170. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  171. Function TCmdLine.GetLaunchingProgram:String;
  172. Begin
  173.   GetLaunchingProgram := LaunchingProgram;
  174. End; { GetLaunchingProgram }
  175. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  176.  
  177.  
  178. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  179. Function TCmdLine.GetParameter(ParmStrIn:String;Var StrBack:String):Boolean;
  180. Const AM:Char = Chr(254);
  181. Var ss,PrmStr:String; ssLen,ParmLen:Integer;
  182.   { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  183.   Procedure SkipQuote(Var xx:Integer;WhichQuote:Char);
  184.   Begin
  185.     Inc(xx);
  186.     While (xx <= ssLen) Do Begin
  187.       If (ss[xx] = WhichQuote) Then Exit;
  188.       Inc(xx);
  189.       End;
  190.   End; { SkipQuote }
  191.   { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  192.   Procedure Setup;
  193.   Var xx,ll:Integer;
  194.   Begin
  195.     ss := CommandLine; MakeCaps(ss);
  196.     PrmStr := ParmStrIn; MakeCaps(PrmStr);
  197.     ssLen := Length(ss); ParmLen := Length(PrmStr);
  198.     { change all dividechars into AMs }
  199.     xx := 0;
  200.     While (xx <= ssLen) Do Begin
  201.        Inc(xx);
  202.        Case ss[xx] Of
  203.          '''','"','`': SkipQuote(xx,ss[xx]);
  204.          Else If (Pos(ss[xx],DivideChars) > 0) Then ss[xx] := AM;
  205.          End; { case }
  206.        End; { while }
  207.   End; { Setup }
  208.   { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  209.   Function IsThisIt(Var Start:Integer):Boolean;
  210.   Var xx:Integer;
  211.   Begin
  212.     IsThisIt := False;
  213.     For xx := 1 to ParmLen Do Begin
  214.       If (ss[Start+xx] <> PrmStr[xx]) Then Exit;
  215.       End; { yy }
  216.     Start := Start+ParmLen;
  217.     IsThisIt := True;
  218.   End; { IsThisIt }
  219.   { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  220.   Function FindIt:Boolean;
  221.   Var xx,yy,l1:Integer;
  222.   Begin
  223.     FindIt := False; StrBack := '';
  224.     l1 := ssLen - ParmLen; If (l1 < 1) Then Exit;
  225.     xx := 0;
  226.     While (xx <= l1) Do Begin
  227.        Inc(xx);
  228.        If (ss[xx] = AM) Then Begin
  229.           If IsThisIt(xx) Then Begin
  230.              FindIt := True; yy := 0;
  231.              { find the next AM, and copy the string out }
  232.              While (xx+yy <= ssLen) And (ss[xx+yy] <> AM) Do Inc(yy);
  233.              StrBack := Copy(CommandLine,xx+1,yy-1);
  234.              { delete trailing space(s), if there }
  235.              While (StrBack[Length(StrBack)] = ' ') Do
  236.                 Delete(StrBack,Length(StrBack),1);
  237.              { we've got the answer, get out }
  238.              Exit;
  239.              End; { this is it }
  240.           End; { found }
  241.        End; { xx }
  242.   End; { FindIt }
  243.   { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  244. Begin
  245.   { default to not found }
  246.   GetParameter := False; StrBack := '';
  247.   If (Length(CommandLine) < 1) or (CommandLine = NONE) Then Exit;
  248.   If (Length(ParmStrIn) < 1) Then Exit;
  249.  
  250.   Setup; GetParameter := FindIt;
  251. End; { GetParameter }
  252. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  253.  
  254.  
  255. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  256. Constructor TCmdLine.Init;
  257.   { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  258.   Function LaunchedBy:String;
  259.   Var ParentSeg:^word; p:pchar;
  260.   Begin
  261.     ParentSeg := ptr(PrefixSeg,$0016); p := ptr(ParentSeg^-1,8);
  262.     LaunchedBy := StrPas(p);
  263.   End; { LaunchedBy }
  264.   { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  265.   Function RealCommandLine:String;
  266.   Var ss:String;
  267.   Begin
  268.     ss := StrPas(ptr(PrefixSeg,130));
  269.     If (Ord(ss[0]) > 0) Then ss[0] := Chr(Ord(ss[0])-1);
  270.     RealCommandLine := ss;
  271.   End; { RealCommandLine }
  272.   { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  273.   Function ActualProgramName:String;
  274.   Var cc:Char; ss:String; p:PChar; xx,yy:Byte;
  275.   Begin
  276.     p := ptr(PrefixSeg,228); ss := ''; xx := 0; yy := 0;
  277.     Repeat
  278.       cc := p[xx];
  279.       If (cc <> #0)
  280.          Then Begin
  281.               If (Ord(cc) > 47) and (Ord(cc) < 126) Then Begin
  282.                  ss := ss+' '; ss[Ord(ss[0])] := p[xx]; End;
  283.               End
  284.          Else Begin Inc(yy); If (yy = 1) Then ss := ss+'.'; End;
  285.       Inc(xx);
  286.       Until (xx > 12) or (yy > 1);
  287.       If (ss[Ord(ss[0])] = '.') Then ss[0] := Chr(Ord(ss[0])-1);
  288.     ActualProgramName := ss;
  289.   End; { ActualProgramName }
  290.   { -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  291. Begin
  292.   LaunchingProgram := LaunchedBy;        { what environment launched me }
  293.   OriginalCommandLine := RealCommandLine;   { the original command line }
  294.   If (Length(OriginalCommandLine) < 1) Then OriginalCommandLine := NONE;
  295.   CommandLine := OriginalCommandLine;                { default to exact }
  296.   CallingProgram := ActualProgramName;     { just what the user entered }
  297.   ActualProgram := ParamStr(0);   { BP returns the fully qualitied name }
  298.  
  299.   SetDivideChars('-/');                   { set the default DivideChars }
  300. End; { Init }
  301. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  302.  
  303.  
  304. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  305. Procedure TCmdLine.Restore;
  306. Begin
  307.   CommandLine := OriginalCommandLine;
  308. End; { Restore }
  309. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  310.  
  311.  
  312. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  313. Procedure TCmdLine.SetDivideChars(NewDivideChars:String);
  314. Begin
  315.   DivideChars := NewDivideChars;
  316. End; { SetDivideChars }
  317. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  318.  
  319.  
  320. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  321. Procedure TCmdLine.Trim;
  322. Begin
  323.   TrimString(CommandLine);
  324. End; { Trim }
  325. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  326.  
  327.  
  328. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  329. Begin { main block }
  330.   CommandLine.Init;
  331. End. { main block }
  332. { =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= }
  333.  
  334.  
  335.  
  336. { This is a test program showing some of the cmdline object functions }
  337. Program whoami;
  338. Uses DOS, CRT, CmdLine;
  339. Var ss:String;
  340. begin
  341.   { show some general information that it returns }
  342.   WriteLn('I was launched by [',CommandLine.GetLaunchingProgram,']');
  343.   WriteLn('Program executed was [',CommandLine.GetCallingProgram,']');
  344.   WriteLn('BP returnd [',CommandLine.GetActualProgram,']');
  345.   WriteLn('Command line was [',CommandLine.GetCommandLine,']');
  346.  
  347.   { these will change the part that you can use }
  348.   CommandLine.Capitalize; CommandLine.Trim;
  349.   WriteLn('Fixed command line [',CommandLine.GetCommandLine,']');
  350.  
  351.   { this will return it to it's original value }
  352.   CommandLine.Restore;
  353.   WriteLn('Restored command line [',CommandLine.GetCommandLine,']');
  354.  
  355.   { check for the existance of some parameter }
  356.   If CommandLine.GetParameter('s',ss)
  357.      Then WriteLn('Parameter "s" was [',ss,']')
  358.      Else WriteLn('Parameter "s" was not found');
  359.   If CommandLine.GetParameter('ss',ss)
  360.      Then WriteLn('Parameter "ss" was [',ss,']')
  361.      Else WriteLn('Parameter "ss" was not found');
  362. end.
  363.  
  364. {
  365. ------------------------------ test.bat ------------------------------
  366. @Echo Off
  367. whoami /a:france/b 'this-is "the" way'  /chest /store /left
  368. whoami /aaa-bbb/s"this 'is'-it"   /sss
  369. whoami -ss/shell
  370.  
  371. }